package com.acc.handler;


import cn.dev33.satoken.exception.NotLoginException;
import com.acc.sys.framework.AccParser;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;


@ControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler({NotLoginException.class})
    public ResponseEntity<JSONObject> handleNotLoginException(NotLoginException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',认证失败'{}',无法访问系统资源", requestURI, e.getMessage());
        return new ResponseEntity<>(AccParser.newResult(401, e.getMessage()), HttpStatus.OK);
    }


    @ExceptionHandler(value = {Exception.class})
    public ResponseEntity<JSONObject> handleException(Exception ex, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}' 错误信息：{}", requestURI, ex.getMessage());
        return new ResponseEntity<>(AccParser.newResult(500, ex.getMessage()), HttpStatus.OK);
    }


}
